package com.caojing.haolemai.ui.collect;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.caojing.haolemai.MyApp;
import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseActivity;
import com.caojing.haolemai.db.CollectBean;
import com.caojing.haolemai.ui.product.ProductActivity;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class CollectActivity extends BaseActivity {


    @Bind(R.id.collect_ivBack)
    ImageView collectIvBack;
    @Bind(R.id.collect_lv)
    ListView collectLv;
    private List<CollectBean> mlist;
    private CollectLvAdapter adapter;
    @Override
    protected void onResume() {
        super.onResume();
        try {
            MyApp.dbUtils.createTableIfNotExist(CollectBean.class);
            mlist= MyApp.dbUtils.findAll(CollectBean.class);
            adapter = new CollectLvAdapter(mlist,this);
            collectLv.setAdapter(adapter);
        } catch (DbException e) {
            e.printStackTrace();
        }
        collectLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String pid = mlist.get(i).getPid();
                Intent intent = new Intent(CollectActivity.this, ProductActivity.class);
                intent.putExtra("id",pid);
                startActivity(intent);
            }
        });
        collectLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
                AlertDialog.Builder builder = new AlertDialog.Builder(CollectActivity.this);
                builder.setTitle("确定要从收藏夹中删除吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                try {
                                    MyApp.dbUtils.delete(CollectBean.class, WhereBuilder.b("pid","=",mlist.get(position).getPid()));
                                    mlist.remove(position);
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(CollectActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                } catch (DbException e) {
                                    e.printStackTrace();
                                }
                            }
                        }).setNegativeButton("取消",null)
                        .show();
                return true;
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_collect;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.collect_ivBack)
    public void onClick() {
        finish();
    }
}
